package com.jinyou.utils.common;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

//分页
public class JYPageUtils {
    public static PageList getList(List<? extends Object> list, Integer page, Integer size) {
        if (ValidateUtil.isNotAbsInteger(page) || page < 1) {
            page = 1;
        }
        if (ValidateUtil.isNotAbsInteger(size) || size < 1) {
            size = 10;
        }
        if (size > 100) {
            size = 100;
        }
        int totalCount = 0;
        if (ValidateUtil.isAbsList(list)) {//分页
            totalCount = list.size();
            if (ValidateUtil.isAbsInteger(page) && ValidateUtil.isAbsInteger(size)) {
                if (list.size() > page * size) {
                    int i = (int) ((page - 1) * size);
                    List<Object> listf = new ArrayList<Object>();
                    for (int j = 0; j < size; j++) {
                        listf.add(list.get(i + j));
                    }
                    return new PageList(totalCount, size, listf);
                } else if (list.size() < page * size && list.size() > (page - 1) * size) {
                    LinkedList<Object> listf = new LinkedList<Object>();
                    for (int j = (int) ((page - 1) * size); j < list.size(); j++) {
                        listf.add(list.get(j));
                    }
                    return new PageList(totalCount, listf.size(), listf);
                } else if (list.size() == page * size) {
                    int i = (int) ((page - 1) * size);
                    List<Object> listf = new ArrayList<Object>();
                    for (int j = 0; j < size; j++) {
                        listf.add(list.get(i + j));
                    }
                    return new PageList(totalCount, size, listf);
                }
            } else {
                List<Object> voList = new ArrayList<Object>();

                if (list.size() < 10) {
                    size = list.size();
                } else {
                    size = 10;
                }
                for (int i = 0; i < size; i++) {
                    voList.add(list.get(i));
                }
                return new PageList(totalCount, voList.size(), voList);
            }


        }

        return new PageList(0, 0, null);
    }


    public static class PageList {
        public int totalCount = 0;
        public int size = 0;
        public List<? extends Object> list = null;

        public PageList() {

        }

        public PageList(int totalCount, int size, List<? extends Object> list) {
            this.totalCount = totalCount;
            this.size = size;
            this.list = list;
        }
    }
}
